//
//C++ - Calculate Covariance and Correlation
//--------------------------------------------------------------------------------
//
//Mean, Variance and Standard Deviation are widely used in statistical application for single series. If we have two sets of series, then we may need covariance and correlation to find the relationship between the two. It is a good idea to start writing program in C++ on this. 
//
//To calculate covariance between two sets of series, we need to multiply the difference between its mean for each term for the two series and add each term resultant value. Finally divide by N - number of terms in the series. 
//
//Correlation coefficient is the ratio between the covariance and the multiplication of the standard deviation for the two series. The range for correlation is always between -1 to +1. 
//
//The complete program compatible in Turbo C++ and Visual C++ Compilers and test run output are given below:


#include <stdio.h>
#include <math.h>
#define ArraySize 1000

class StdDeviation

{ 

private:

    int max;

    float value[ArraySize]; // Larger than Slices

    float mean;

    

public:

 

    float CalculateMean()

    {

        float sum = 0; 

        for(int i = 0; i < max; i++)

            sum += value[i];

        return (sum / max);

    }

 

    float CalculateVariane()

    {

        mean = CalculateMean();

 

        float temp = 0; 

        for(int i = 0; i < max; i++)

        {

             temp += (value[i] - mean) * (value[i] - mean) ;

        }

        return temp / max;

    }

 

    float CalculateSampleVariane()

    {

        mean = CalculateMean();

 

        float temp = 0; 

        for(int i = 0; i < max; i++)

        {

             temp += (value[i] - mean) * (value[i] - mean) ;

        }

        return temp / (max - 1);

    }

 

    int SetValues(float *p, int count)

    {

        if(count > ArraySize)

            return -1;

        max = count;

        for(int i = 0; i < count; i++)

            value[i] = p[i];

        return 0;

    }

 

    float Calculate_StandardDeviation()

    {

        return sqrt(CalculateVariane());

    }

 

    float Calculate_SampleStandardDeviation()

    {

        return sqrt(CalculateSampleVariane());

    }

 

};

 

class StdCalculator

{

private:

    float XSeries[200];// Larger than Slices

    float YSeries[200];// Larger than Slices

    int max;

 

    StdDeviation x;

    StdDeviation y;

 

public:

    void SetValues(float *xvalues, float *yvalues, int count)

    {

 

        for(int i = 0; i < count; i++)

        {

            XSeries[i] = xvalues[i];

            YSeries[i] = yvalues[i];

        }

        x.SetValues(xvalues, count);

        y.SetValues(yvalues, count);

        max = count;

    }

 

    float Calculate_Covariance()

    {

        float xmean = x.CalculateMean();

        float ymean = y.CalculateMean();

 

        float total = 0;

        for(int i = 0; i < max; i++)

        {

            total += (XSeries[i] - xmean) * (YSeries[i] - ymean);

        }

        return total / max;

    }

 

    float Calculate_Correlation()

    {

		float cov = Calculate_Covariance();

		float correlation;

		if (cov==0)

			correlation =0;

		else

			correlation = cov / (x.Calculate_StandardDeviation() * y.Calculate_StandardDeviation());

		return correlation;

    }

};


//void main()
//
//{
//
//    FinanceCalculator calc;
//
// 
//
//    {
//
//        printf("\n\nZero Correlation and Covariance Data Set\n");
//
//        float xarr[] = { 8, 6, 4, 6, 8 };
//
//        float yarr[] = { 10, 12, 14, 16, 18 };
//
// 
//
//        calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));
//
// 
//
//        printf("Covariance = %.10lf\n", calc.Calculate_Covariance());
//
//        printf("Correlation = %.10lf\n", calc.Calculate_Correlation());
//
//    }
//
// 
//
//    {
//
//        printf("\n\nPositive Correlation and Low Covariance Data Set\n");
//
//        float xarr[] = { 0, 2, 4, 6, 8 };
//
//        float yarr[] = { 6, 13, 15, 16, 20 };
//
// 
//
//        calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));
//
// 
//
//        printf("Covariance = %.10lf\n", calc.Calculate_Covariance());
//
//        printf("Correlation = %.10lf\n", calc.Calculate_Correlation());
//
//    }
//
//    {
//
//        printf("\n\nNegative Correlation and Low Covariance Data Set\n");
//
//        float xarr[] = { 8, 6, 4, 2, 0 };
//
//        float yarr[] = { 6, 13, 15, 16, 20 };
//
// 
//
//        calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));
//
// 
//
//        printf("Covariance = %.10lf\n", calc.Calculate_Covariance());
//
//        printf("Correlation = %.10lf\n", calc.Calculate_Correlation());
//
//    }
//
// 
//
//    {
//
//        printf("\n\nPositive Correlation and High Covariance Data Set\n");
//
//        float xarr[] = { 8, 6, 4, 2, 0 };
//
//        float yarr[] = { 1006, 513, 315, 216, 120 };
//
// 
//
//        calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));
//
// 
//
//        printf("Covariance = %.10lf\n", calc.Calculate_Covariance());
//
//        printf("Correlation = %.10lf\n", calc.Calculate_Correlation());
//
//    }
//
//    {
//
//        printf("\n\nNegative Correlation and High Covariance Data Set\n");
//
//        float xarr[] = { 8, 6, 4, 2, 0 };
//
//        float yarr[] = { 120, 216, 315, 513, 1006 }; 
//
// 
//
//        calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));
//
// 
//
//        printf("Covariance = %.10lf\n", calc.Calculate_Covariance());
//
//        printf("Correlation = %.10lf\n", calc.Calculate_Correlation());
//
//    }
//
//}
//
// 
//
//Output
//
//--------------------------------------------------------------------------------
//Zero Correlation and Covariance Data Set
//
//Covariance = 0.0000000000
//
//Correlation = 0.0000000000
//
//  
//
// 
//
//Positive Correlation and Low Covariance Data Set
//
//Covariance = 12.4000000000
//
//Correlation = 0.9521574311
//
// 
//
// 
//
//Negative Correlation and Low Covariance Data Set
//
//Covariance = -12.4000000000
//
//Correlation = -0.9521574311
//
// 
//
// 
//
//Positive Correlation and High Covariance Data Set
//
//Covariance = 827.6000000000
//
//Correlation = 0.9311642376
//
// 
//
// 
//
//Negative Correlation and High Covariance Data Set
//
//Covariance = -827.6000000000
//
//Correlation = -0.9311642376
